<?php

date_default_timezone_set('Asia/Saigon');

class ManagerController extends Controller {

    public $layout = '//layouts/manager';

    public function actionIndex() {

        Yii::app()->clientScript->registerMetaTag('salesmartlink,to chuc, chung nhan, the tieu dung thong minh, pr thuong hieu, san pham, dich vu, y te, bao hiem, oto, xe may, lam dep, my pham, thoi trang, thiet ke web, in an', 'keywords');
        Yii::app()->clientScript->registerMetaTag('Cổng thông tin điện tử saleSmartlink.com là Tổ chức chứng nhận và giám sát hoạt động bán hàng khuyến mãi đầu tiên tại Việt Nam thông qua việc phát hành thẻ Tiêu dùng Thông minh – saleSmartlink Card tích hợp tiện ích tài khoản ngân hàng cho người tiêu dùng trong nước và quốc tế. salesamrtlink là hệ thống thông tin dịch vụ, chúng tôi luôn đồng hành với các doanh nghiệp trong các dịch vụ tìm kiếm khách hàng và chúng tôi luôn bảo vệ lợi ích của khách hàng.', 'description');
        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $this->render('index');
    }

    public function actionLichsugiaodich() {

        $this->render('ca-nhan');
    }

    public function actionList_game() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        //$userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $this->redirect('http://salesmartlink.com/');
        } else {
            $criteria->condition = ' flag <2';
        }
        $program = Program::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'program' => $program,));
    }

    public function actionList_slider() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and UserID =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $slider = Slider::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'slider' => $slider,));
    }

    public function actionList_Panel() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $panel = Panel::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'panel' => $panel,));
    }

    public function actionList_Page() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $page = Page::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'page' => $page,));
    }

    public function actionList_People() {
        $results = '';
        if (isset($_POST['gid'])) {
            $id = $_POST['gid'];

            $sql = 'SELECT t1.id,t4.username,t1.position,t1.date_log,t2.title,t3.name ' .
                    ' FROM `program_detail` AS t1 ' .
                    ' INNER JOIN `program` AS t2 on t1.programid =t2.gid ' .
                    ' INNER JOIN `phanqua` AS t3 on t1.idqua =t3.id ' .
                    ' INNER JOIN  `user_tb` AS t4 ON t1.userid = t4.uid' .
                    ' where t1.programid=' . $id;
            $dbCommand = Yii::app()->db->createCommand($sql);
            $results = $dbCommand->queryAll();
        } $this->render('ca-nhan', array(
            'results' => $results,));
    }

    public function actionMuaquangcao() {

        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $model = new Apay();
        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionMuaquangcao1() {

        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $model = new Apay(); //0974598708
        if (isset($_POST['Apay'])) {
            $model->attributes = $_POST['Apay'];
            $session = new CHttpSession;
            $session->open();
            $p = $session['thang'];
            $gia = $this->loadModel_config($model->OrderNo);
            if ((int) $p > 12) {
                $m = '2 năm';
            } elseif ((int) $p > 11) {
                $m = '1 năm';
            } else {
                $m = $p . ' tháng';
            }

            $value = $gia->value;

            $tota = $value * $p;
            $model->OrderCashAmount = $tota;
            $model->code = $model->OrderNo . date('Ymdhis');
            $arr = array($model->OrderNo . date('Ymdhis'), $tota, 'Thanh toán mua ' . $gia->name . ' online qua ví điện tử payoo tại salemartlink.com', $gia->name, number_format($value), $m, number_format($tota));
            $session['user_'] = $arr;
            $model->uid = $session['user_salesmart'][3];
            $model->daytime = new CDbExpression('NOW()');
            $model->state = 0;
            $model->thang = $p;
            if ($model->save()) {
                $session['chekout'] = $arr;
                $url = Yii::app()->createUrl('check-out');
                $this->redirect($url);
            }
        } else {
            $this->render('ca-nhan', array(
                'model' => $model,
            ));
        }
    }

    public function actionMuaquangcaoOk() {

        $this->pageTitle = 'Thẻ tiêu dùng thông minh';


        $model = 'Xin chúc mừng bạn đã mua quảng cáo thành công';
        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionMuaquangcaoFalse() {

        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $model = 'Thanh toán thất bại. Vui lòng thử lại';
        $url = Yii::app()->createUrl('mua-quang-cao');
        $this->redirect($url);
    }

    public function loadModel_config($id) {
        $model = Config::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionTaikhoan() {
        //Home page
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $canhan = 'true';
        $this->render('ca-nhan', array(
            'canhan' => $canhan,
        ));
    }

    public function actionDangky() {
        //$txtname = $_POST['txtname'];
        //$txtemail = $_POST['txtemail'];
        //$txtphone = $_POST['txtphone'];
        //$txtadress = $_POST['txtadress'];
        // $pwd = $_POST['pwd'];
        /*
          $db_host = "localhost"; // Giữ mặc định là localhost
          $db_name        = 'demo';// Can thay doi
          $db_username    = 'root'; //Can thay doi
          $db_password    = 'laivt';//Can thay doi
          @mysql_connect("{$db_host}", "{$db_username}", "{$db_password}") or die("Không thể kết nối database");
          @mysql_select_db("{$db_name}") or die("Không thể chọn database");
         */
        $session = new CHttpSession;
        $session->open();

        if (!empty($session['user_salesmart'])) {
            $this->redirect('http://salesmartlink.com/');
        }
        $model = new UserTb;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        $ketqua = 0;
        $this->pageTitle = 'Đăng ký';
        if (isset($_POST['UserTb'])) {
            $model->attributes = $_POST['UserTb'];
            $model->flag = 1;
            $model->registerdate = new CDbExpression('NOW()');
            $model->pass = md5(md5($model->pass));

            if ($model->save()) {

                $this->redirect('thong-bao/1');
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddSlider() {

        $model = new Slider;
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo slider';
        if (isset($_POST['Slider'])) {
            //$the_image = CUploadedFile::getInstance($model, 'photo');

            /*  if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {
              $model->photo = $the_image;
              } */
            $model->attributes = $_POST['Slider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }
            //$model->photo=CUploadedFile::getInstance($model,'photo');
            $model->flag = 0;
            $model->dateCreate = new CDbExpression('NOW()');
            $model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->UserID = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] > 98) {
                $model->flag = 1;
            }
            /*
              foreach ($model as $item) {

              $item->UserTb->flag = $flag;
              } */
            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                // $uploadFolder = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . substr(md5($the_image), 0, 1); //thư mục upload là thư mục nằm trong project
                //if (!is_dir($uploadFolder)) {
                // mkdir($uploadFolder, 0775, TRUE); //tạo thư mục nếu chưa tồn tại
                //}
                // $uploadFile = $uploadFolder . DIRECTORY_SEPARATOR . $the_image;
                //$model->photo->saveAs(Yii::getPathOfAlias('webroot') . '/upload/');
                //$this->redirect(array('view','id'=>$model->id));

                $ketqua = 1;
                //$url = Yii::app()->createUrl('');
                //$this->redirect($url);
                $url = Yii::app()->createUrl('danh-sach-slider');
                $this->redirect($url);
                //$this->render('//manager/list-slider', array(
                // 'ketqua' => $ketqua,
                //));
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddPage() {

        $model = new Page();

        $this->pageTitle = 'Tạo Page';
        if (isset($_POST['Page'])) {

            $model->attributes = $_POST['Page'];
            $model->datecreate = new CDbExpression('NOW()');
            $model->dateup = new CDbExpression('NOW()');

            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }

            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-page');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePage($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_page($md->decrypt($id));
        if (isset($_POST['Page'])) {
            $model->attributes = $_POST['Page'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-page');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePage($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_page($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-page');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-page');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddPanel() {

        $model = new Panel();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['Panel'])) {

            $model->attributes = $_POST['Panel'];
            $uploaded = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->photo = $fileName;
            }

            $model->flag = 0;
            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] > 98) {
                $model->flag = 1;
            }
            if ($session['user_salesmart'][1] < 99) {
                $kiemtra = Apay::model()->findAll(' `uid`=' . $session['user_salesmart'][3] . ' and  `OrderNo`=' . $model->type . ' and `dayend` >= "' . date('Y-m-d') . '" order by id DESC');
                if (empty($kiemtra)) {
                    $ketqua = 1;
                }
            }
            if ($ketqua == 0) {
                if ($model->save()) {
                    $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/upload/quangcao/' . $model->photo);

                    $url = Yii::app()->createUrl('danh-sach-panel');
                    $this->redirect($url);
                }
            } 
        }

        $this->render('ca-nhan', array(
            'model' => $model,
            'ketqua' => $ketqua,
        ));
    }

    public function actionUpdatePanel($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panel($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['Panel'])) {
            $model->attributes = $_POST['Panel'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/quangcao/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-panel');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePanel($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panel($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-panel');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-panel');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionList_Product() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $product = Product::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'product' => $product,));
    }

    public function actionAddProduct() {

        $model = new Product();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['Product'])) {

            $model->attributes = $_POST['Product'];
            $uploaded = CUploadedFile::getInstance($model, 'avatar');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->avatar = $fileName;
            }

            $model->pr_datecreate = new CDbExpression('NOW()');
            $model->pr_dateup = new CDbExpression('NOW()');

            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 98) {
                $model->flag = 0;
            }

            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/avata/' . $model->avatar);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-san-pham');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateProduct($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_product($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->avatar;
        $up = 0;
        if (isset($_POST['Product'])) {
            $model->attributes = $_POST['Product'];
            $uploadedFile = CUploadedFile::getInstance($model, 'avatar');
            $model->avatar = $oldphoto;
            $session = new CHttpSession;
            $session->open();

            // $model->uid = $session['user_salesmart'][3];
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->avatar = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/avata/' . $model->avatar);
                }
                $url = Yii::app()->createUrl('danh-sach-san-pham');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteProduct($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_product($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-san-pham');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-san-pham');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddGame() {

        $model = new Program;

        $ketqua = 0;
        $this->pageTitle = 'Tạo chương trình';
        if (isset($_POST['Program'])) {

            $model->attributes = $_POST['Program'];
            if ($model->save()) {
                $ketqua = 1;
                $url = Yii::app()->createUrl('Chuong-trinh-game-show');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddPhanqua() {

        $model = new Phanqua();

        $ketqua = 0;
        $this->pageTitle = 'Tạo phần quà';
        if (isset($_POST['Phanqua'])) {

            $model->attributes = $_POST['Phanqua'];
            if ($model->save()) {
                $ketqua = 1;
                $url = Yii::app()->createUrl('cai-dat-qua');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePhanqua($id) {

        $this->pageTitle = 'Quản lý phần quà';
        $thongbao = '';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_phanqua($md->decrypt($id));

        if (isset($_POST['Phanqua'])) {
            $model->attributes = $_POST['Phanqua'];
            if ($model->save()) {
                $thongbao = 'Thông tin của bạn đã được cập nhật';
            }
        }
        $this->render('ca-nhan', array(
            'model' => $model,
            'thongbao' => $thongbao,
        ));
    }

    public function actionUpdateProgram($id) {

        $this->pageTitle = 'Quản lý chương trình';
        $thongbao = '';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_program($md->decrypt($id));

        if (isset($_POST['Program'])) {
            $model->attributes = $_POST['Program'];
            if ($model->save()) {
                $thongbao = 'Thông tin của bạn đã được cập nhật';
            }
        }
        $this->render('ca-nhan', array(
            'model' => $model,
            'thongbao' => $thongbao,
        ));
    }

    public function actionUpdateUser() {

        $this->pageTitle = 'Thông tin cá nhân';
        $session = new CHttpSession;
        $session->open();
        $id = $session['user_salesmart'][3];
        $thongbao = '';
        $model = $this->loadModel_user($id);
        $pass = $model->pass;
        $registerdate = $model->registerdate;
        $email = $model->email;
        if (isset($_POST['UserTb'])) {
            $model->attributes = $_POST['UserTb'];

            $model->pass = $pass;
            $model->registerdate = $registerdate;
            $model->email = $email;
            if ($model->save()) {
                $thongbao = 'Thông tin của bạn đã được cập nhật';
            }
        }
        $this->render('ca-nhan', array(
            'model' => $model,
            'thongbao' => $thongbao,
        ));
    }

    public function actionChangepass() {

        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $session = new CHttpSession;
        $session->open();
        $id = $session['user_salesmart'][3];
        $thongbao = '';



        if (isset($_POST['pass']) && isset($_POST['rpwd']) && isset($_POST['olpass'])) {

            if ($_POST['pass'] === $_POST['rpwd']) {
                $p = $_POST['pass'];
                $rp = $_POST['olpass'];
                $model = $this->loadModel_user($id);
                if ($model->pass === md5(md5($rp))) {
                    $model->pass = md5(md5($p));
                    if ($model->save()) {
                        $thongbao = 'Thông tin của bạn đã được cập nhật';
                        $this->redirect('ca-nhan', array(
                            'thongbao' => $thongbao,));
                    }
                } else {
                    $thongbao = 'Mật khẩu cũ không đúng';
                }
            } else {
                $thongbao = 'Mật khẩu nhập lại không đúng';
            }
        }
        $this->render('ca-nhan', array(
            'thongbao' => $thongbao,
        ));
    }

    public function actionUpdateSlider($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slider($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['Slider'])) {
            $model->attributes = $_POST['Slider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-slider');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteSlider($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slider($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->UserID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-slider');
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('danh-sach-slider');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionDeleteProgam($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_program($md->decrypt($id));
        $model->flag = 3;

        if ($model->save()) {
            $url = Yii::app()->createUrl('Chuong-trinh-game-show');
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('Chuong-trinh-game-show');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionXoaPhanqua($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_phanqua($md->decrypt($id));
        $model->flag = 3;

        if ($model->save()) {
            $url = Yii::app()->createUrl('cai-dat-qua');
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('cai-dat-qua');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */

    public function loadModel_phanqua($id) {
        $model = Phanqua::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_program($id) {
        $model = Program::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_panel($id) {
        $model = Panel::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_page($id) {
        $model = Page::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_slider($id) {
        $model = Slider::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_product($id) {
        $model = Product::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_user($id) {
        $model = UserTb::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    //shop shop 
    public function actionDynamicvalue() {
        $session = new CHttpSession;
        $session->open();
        $session['trapos'] = '1';
        $data = Config::model()->findAll('flag=1 and id=:id', array(':id' => (int) $_POST['id']));
        $data = CHtml::listData($data, 'id', 'value');
        foreach ($data as $value => $name) {
            //echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
            $session['loaiqc'] = $name;
            echo CHtml::textField('Text', number_format(CHtml::encode($name)), array('id' => 'qcso', 'width' => 100, 'maxlength' => 100, 'readOnly' => true));
        }
    }

    public function actionDynamicvalue1() {
        $session = new CHttpSession;
        $session->open();
        if (isset($session['loaiqc'])) {
            if (!empty($session['loaiqc'])) {
                $vlu = $session['loaiqc'];
                $session['thang'] = (int) $_POST['id'];
                $k = (int) $_POST['id'] * (int) $vlu;
                echo CHtml::textField('Text', number_format(CHtml::encode($k)), array('id' => 'qcso', 'width' => 100, 'maxlength' => 100, 'readOnly' => true));
            }
        }
    }

    public function actionDynamicvalueloai() {
        //$session = new CHttpSession;
        // $session->open();
        // $session['hinhthuc'] = (int) $_POST['id'];

        $k = (int) $_POST['id'];
        if ($k == 999) {
            echo '<input value="0" style="width:80px;" name="Product[discount]" id="Product_discount" type="text">';
        } else {
            echo '<input type="hidden" value="' . $k . '" style="width:80px;" name="Product[discount]" id="Product_discount" type="text">';
        }
    }

    public function actionDynamicMenushop() {
        $data = ShopMenu::model()->findAll('parentID=0 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn menu cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionDynamicCategory() {
        $data = ShopNavbar::model()->findAll('parent=0 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionDynamicProduct() {
        $data = ShopNavbar::model()->findAll('shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        foreach ($data as $item) {
            if ($item->parent > 0) {
                $item->title = '   + ' . $item->title;
            }
        }
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        //echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionDynamicCity() {
        $data = ShopCity::model()->findAll('flag=1 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        // $value0 = 0;
        $data = CHtml::listData($data, 'id', 'cityName');
        //echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionList_Shop() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();
        if (!isset($session['user_salesmart'])) {
            $this->redirect('http://www.salesmartlink.com/');
        }
        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag in(1,4) and uid =' . $userID;
        } else {
            $criteria->condition = ' flag in(1,2,4)';
        }
        $shop = Shop::model()->findAll($criteria);
        $this->render('///manager/ca-nhan', array(
            'shop' => $shop,));
    }

    public function actionAddShop() {

        $model = new Shop();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Tạo Gian Hàng';
        if (isset($_POST['Shop'])) {

            $model->attributes = $_POST['Shop'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->logo = $fileName;
            }
            $rnd = rand(0, 99999);
            $uploadedFile1 = CUploadedFile::getInstance($model, 'avata');
            $fileName1 = "{$rnd}-{$uploadedFile1}";  // random number + file name
            if (!empty($uploadedFile1)) {
                $model->avata = $fileName1;
            }

            $model->datecreate = new CDbExpression('NOW()');

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            //if ($session['user_salesmart'][1] < 99) {
            // $model->flag = 0;
            // }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->logo);
                $uploadedFile1->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->avata);
                $url = Yii::app()->createUrl('danh-sach-shop');
                $this->redirect($url);
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateShop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shop($md->decrypt($id));
        $rnd = rand(0, 99999);
        $oldphoto = $model->logo;
        $oldphoto1 = $model->avata;
        if (isset($_POST['Shop'])) {
            $model->attributes = $_POST['Shop'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $model->logo = $oldphoto;
            $up = 0;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->logo = $fileName;
            }
            $rnd = rand(0, 99999);
            $uploadedFile1 = CUploadedFile::getInstance($model, 'avata');
            $model->avata = $oldphoto1;
            $up1 = 0;
            if (!empty($uploadedFile1)) {
                $fileName1 = "{$rnd}-{$uploadedFile1}";  // random number + file name
                $up1 = 1;
                $model->avata = $fileName1;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->logo);
                }
                if ($up1 > 0) {
                    $uploadedFile1->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->avata);
                }
                $url = Yii::app()->createUrl('danh-sach-shop');
                $this->redirect($url);
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteShop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-shop');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-shop');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListPageshop() {
        $this->pageTitle = 'Quản lý bài viết';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsPageshop($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopPage::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Danh sách bài viét';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddPageshop() {

        $model = new ShopPage();

        $this->pageTitle = 'Tạo Page';
        if (isset($_POST['ShopPage'])) {

            $model->attributes = $_POST['ShopPage'];


            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];


            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePageshop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_pageshop($md->decrypt($id));
        if (isset($_POST['ShopPage'])) {
            $model->attributes = $_POST['ShopPage'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePageshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_pageshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        //$model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddCity() {

        $model = new ShopCity();

        $this->pageTitle = 'Thêm tỉnh/thành';
        if (isset($_POST['ShopCity'])) {

            $model->attributes = $_POST['ShopCity'];
            $session = new CHttpSession;
            $session->open();
            $model->shopid = $session['user_salesmart'][3];

            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-city');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateCity($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Cityshop($md->decrypt($id));
        if (isset($_POST['ShopCity'])) {
            $model->attributes = $_POST['ShopCity'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-city');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteCity($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Cityshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-city');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-city');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListMenu() {
        $this->pageTitle = 'Quản lý menu';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsMenu($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopMenu::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm menu';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddMenu() {

        $model = new ShopMenu();

        $this->pageTitle = 'Thêm menu';
        if (isset($_POST['ShopMenu'])) {

            $model->attributes = $_POST['ShopMenu'];
            $session = new CHttpSession;
            $session->open();
            $model->uid = $session['user_salesmart'][3];
            if ($model->parentID == NULL) {
                $model->parentID = 0;
            }

            if ($model->save()) {

                $url = Yii::app()->createUrl('gian-hang-menu');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateMenu($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Menushop($md->decrypt($id));
        if (isset($_POST['ShopMenu'])) {
            $model->attributes = $_POST['ShopMenu'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('gian-hang-menu');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteMenu($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Menushop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('gian-hang-menu');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('gian-hang-menu');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListCategory() {
        $this->pageTitle = 'Quản lý danh mục';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsCategory($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopNavbar::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm danh mục';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddCategory() {

        $model = new ShopNavbar();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Thêm danh mục';
        if (isset($_POST['ShopNavbar'])) {

            $model->attributes = $_POST['ShopNavbar'];
            $uploadedFile = CUploadedFile::getInstance($model, 'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->image = $fileName;
            }


            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            //if ($session['user_salesmart'][1] < 99) {
            // $model->flag = 0;
            // }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->image);
                $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateCategory($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shopCategory($md->decrypt($id));
        $rnd = rand(0, 99999);
        $oldphoto = $model->image;
        if (isset($_POST['ShopNavbar'])) {
            $model->attributes = $_POST['ShopNavbar'];
            $uploadedFile = CUploadedFile::getInstance($model, 'image');
            $model->image = $oldphoto;
            $up = 0;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->image = $fileName;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->image);
                }
                $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteCategory($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shopCategory($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListSlider() {
        $this->pageTitle = 'Quản lý danh mục';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsSlider($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopSlider::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm danh mục';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddSliderShop() {

        $model = new ShopSlider;
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo slider';
        if (isset($_POST['ShopSlider'])) {

            $model->attributes = $_POST['ShopSlider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }

            $model->dateCreate = new CDbExpression('NOW()');
            $model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/slider/' . $model->photo);

                $ketqua = 1;

                $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateSliderShop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slidershop($md->decrypt($id));

        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['ShopSlider'])) {
            $model->attributes = $_POST['ShopSlider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteSliderShop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slidershop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        //$model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListpanelshop() {
        $this->pageTitle = 'Quản lý quảng cáo';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsPanelshop($id) {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID . ' and shopid=' . $id;
        } else {
            $criteria->condition = ' flag <3 and shopid=' . $id;
        }
        $model = ShopPanel::model()->findAll($criteria);
        $this->render('//manager/ca-nhan', array(
            'model' => $model,));
    }

    public function actionAddPanelshop() {

        $model = new ShopPanel();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['ShopPanel'])) {

            $model->attributes = $_POST['ShopPanel'];
            $uploaded = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->photo = $fileName;
            }

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];


            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/quangcao/' . $model->photo);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePanelshop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panelshop($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['ShopPanel'])) {
            $model->attributes = $_POST['ShopPanel'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/quangcao/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePanelshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panelshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        // $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListProductshop() {
        $this->pageTitle = 'Quản lý sản phẩm';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsProductshop($id) {

        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID . ' and shopid=' . $id;
        } else {
            $criteria->condition = ' flag <3 and shopid=' . $id;
        }
        $model = ShopProduct::model()->findAll($criteria);

        $this->pageTitle = 'Danh sách sản phẩm';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddProductshop() {

        $model = new ShopProduct();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo sản phẩm';
        if (isset($_POST['ShopProduct'])) {

            $model->attributes = $_POST['ShopProduct'];
            $uploaded = CUploadedFile::getInstance($model, 'avatar');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->avatar = $fileName;
            }

            $model->pr_datecreate = new CDbExpression('NOW()');
            $model->pr_dateup = new CDbExpression('NOW()');
            $model->pr_stardate = new CDbExpression('NOW()');
            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 98) {
                $model->flag = 0;
            }

            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/product/' . $model->avatar);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateProductshop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_productshop($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->avatar;
        $up = 0;
        if (isset($_POST['ShopProduct'])) {
            $model->attributes = $_POST['ShopProduct'];
            $uploadedFile = CUploadedFile::getInstance($model, 'avatar');
            $model->avatar = $oldphoto;
            $session = new CHttpSession;
            $session->open();

            //$model->uid = $session['user_salesmart'][3];
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->avatar = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/product/' . $model->avatar);
                }
                $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteProductshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_productshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function loadModel_panelshop($id) {
        $model = ShopPanel::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_pageshop($id) {
        $model = ShopPage::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Cityshop($id) {
        $model = ShopCity::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Menushop($id) {
        $model = ShopMenu::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_slidershop($id) {
        $model = ShopSlider::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_productshop($id) {
        $model = ShopProduct::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_shop($id) {
        $model = Shop::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_shopCategory($id) {
        $model = ShopNavbar::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
